package com.bigdata.assignment.problem1;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

/**
 * WordCountReducer
 * ----------------
 * 功能：接收 Mapper 输出的相同单词的所有计数，对其进行累加。
 *
 * 核心步骤：
 * 1. 初始化计数器
 * 2. 遍历所有 IntWritable 值累加求和
 * 3. 输出 (word, total_count)
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private IntWritable result = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {

        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        result.set(sum);
        context.write(key, result);
    }
}

